﻿// <copyright file="ArmyAddEditGM.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Collections.Generic;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class ArmyAddEditGM
    {
        partial void ArmyAddEditGM_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.ArmyId == null)
            {
                this.Army = new Army();
                this.Army.Visibility = (int)VisibilityEnum.Public;
                this.DisplayName = "Add new Army";
            }
            else
            {
                this.Army = this.ArmyQuery;
                this.DisplayName = string.Format("Edit {0}", this.Army.Name);
            }
        }

        #region AddExisting/Remove Character

        partial void CharactersAddExisting_Execute()
        {
            this.CharacterToAddToArmy = null;
            this.FindControl("ModalWindowCharacter").DisplayName = "Add existing Character";
            this.OpenModalWindow("ModalWindowCharacter");
        }

        partial void SubmitModalWindowCharacter_CanExecute(ref bool result)
        {
            result = this.CharacterToAddToArmy != null;
        }

        partial void SubmitModalWindowCharacter_Execute()
        {
            this.CloseModalWindow("ModalWindowCharacter");
            this.Army.Characters.Add(this.CharacterToAddToArmy);
            this.DataWorkspace.ApplicationData.SaveChanges();

            // Refresh the involved queries
            this.Characters.Refresh();
        }

        partial void CancelModalWindowCharacter_Execute()
        {
            this.CloseModalWindow("ModalWindowCharacter");
        }

        #endregion

        #region Add/Edit Land Unit

        partial void LandUnitsAddAndEditNew_Execute()
        {
            var unit = this.LandUnits.AddNew();
            unit.Visibility = (int)VisibilityEnum.Public;
            unit.IsActive = true;

            this.FindControl("ModalWindowLandUnit").DisplayName = "Add new Land Unit";
            this.OpenModalWindow("ModalWindowLandUnit");
        }

        partial void LandUnitsEditSelected_CanExecute(ref bool result)
        {
            result = this.LandUnits.SelectedItem != null;
        }

        partial void LandUnitsEditSelected_Execute()
        {
            this.FindControl("ModalWindowLandUnit").DisplayName = string.Format("Edit {0}", this.LandUnits.SelectedItem.Name);
            this.OpenModalWindow("ModalWindowLandUnit");
        }

        partial void SubmitModalWindowLandUnit_Execute()
        {
            this.CloseModalWindow("ModalWindowLandUnit");
        }

        partial void CancelModalWindowLandUnit_Execute()
        {
            this.CloseModalWindow("ModalWindowLandUnit");
            this.LandUnits.SelectedItem.Details.DiscardChanges();
        }

        #endregion

        #region Add/Edit Sea Unit

        partial void SeaUnitsAddAndEditNew_Execute()
        {
            var unit = this.SeaUnits.AddNew();
            unit.Visibility = (int)VisibilityEnum.Public;
            unit.IsActive = true;

            this.FindControl("ModalWindowSeaUnit").DisplayName = "Add new Sea Unit";
            this.OpenModalWindow("ModalWindowSeaUnit");
        }

        partial void SeaUnitsEditSelected_CanExecute(ref bool result)
        {
            result = this.SeaUnits.SelectedItem != null;
        }

        partial void SeaUnitsEditSelected_Execute()
        {
            this.FindControl("ModalWindowSeaUnit").DisplayName = string.Format("Edit {0}", this.SeaUnits.SelectedItem.Name);
            this.OpenModalWindow("ModalWindowSeaUnit");
        }

        partial void SubmitModalWindowSeaUnit_Execute()
        {
            this.CloseModalWindow("ModalWindowSeaUnit");
        }

        partial void CancelModalWindowSeaUnit_Execute()
        {
            this.CloseModalWindow("ModalWindowSeaUnit");
            this.SeaUnits.SelectedItem.Details.DiscardChanges();
        }

        #endregion
    }
}